package com.extrace.server.controller;

import com.extrace.server.config.AjaxResult;
import com.extrace.server.mapper.AdminMapper;
import com.extrace.server.pojo.Admin;
import com.extrace.server.pojo.User;
import com.extrace.server.realm.MyToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
@CrossOrigin
@RestController
public class AdminController {

    @Resource
    AdminMapper adminMapper;
    @PostMapping(value = "/admin/login")
    public AjaxResult login(@RequestBody Admin admin) {
        Admin admin1 = adminMapper.selectByUsername(admin.getUsername());
        if(admin1==null){
            return AjaxResult.success("用户名或者密码错误!");
        }
        if(admin.getPassword().equals(admin1.getPassword())) {
            return AjaxResult.success("登录成功");
        } else {
            return AjaxResult.success("用户名或者密码错误!");
        }
    }

    @PostMapping(value = "/admin/register")
    public AjaxResult register(@RequestBody Admin admin) {
        Admin admin1 = adminMapper.selectByUsername(admin.getUsername());
        if(admin1 != null){
            return AjaxResult.success("用户名已经存在");
        }
        Integer result = adminMapper.insert(admin);
        if(result>0){
            return AjaxResult.success("用户创建成功");
        } else {
            return AjaxResult.success("用户创建失败");
        }
    }
}
